const db = require("../db/index");
// 导入加密中间件
const bcrypt = require("bcryptjs");
const crypto = require("crypto");
const fs = require("fs");

const userInfoController = {
  // 修改头像接口
  uploadAvatar: async (req, res) => {
    const onlyId = crypto.randomUUID();
    let oldName = req.files[0].filename;
    let newName = Buffer.from(req.files[0].originalname, "latin1").toString(
      "utf-8"
    );
    fs.renameSync("./public/upload/" + oldName, "./public/upload/" + newName);
    const sql = "INSERT INTO image SET ?";
    try {
      await db.query(sql, {
        image_url: `http://127.0.0.1:3000/upload/${newName}`,
        onlyId,
      });
    } catch (err) {
      return res.cc(err);
    }
    res.send({
      onlyId,
      status: 1,
      url: `http://127.0.0.1:3000/upload/${newName}`,
    });
  },
  // 绑定头像接口
  bingAccount: async (req, res) => {
    const { account, onlyId, url } = req.body;
    const sql = "UPDATE image SET account = ? WHERE onlyId = ?";
    const [data] = await db.query(sql, [account, onlyId]);
    if (data.affectedRows === 1) {
      const sql1 = "UPDATE users SET image_url = ? WHERE account = ?";
      await db.query(sql1, [url, account]);
      return res.send({
        status: 1,
        message: "修改成功",
      });
    } else {
      res.cc("参数错误");
    }
  },
  // 修改密码接口
  updatePassword: async (req, res) => {
    let { id, newPassword, oldPassword } = req.body;
    const sql = "SELECT password FROM users WHERE id = ?";
    const [data] = await db.query(sql, id);
    if (!data.length) {
      res.cc("未找到该用户");
    }
    const compareResult = bcrypt.compareSync(oldPassword, data[0].password);
    if (!compareResult) {
      res.cc("原密码输入错误");
    }
    const newcompareResult = bcrypt.compareSync(newPassword, data[0].password);
    if (newcompareResult) {
      res.cc("新旧密码相同");
    }
    newPassword = bcrypt.hashSync(newPassword, 10);
    const sql1 = "UPDATE users SET password = ? WHERE id = ?";
    try {
      await db.query(sql1, [newPassword, id]);
      res.send({
        status: 1,
        message: "修改成功",
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 获取用户信息接口
  getUserInfo: async (req, res) => {
    const sql = "SELECT * FROM users WHERE id = ?";
    const [data] = await db.query(sql, req.query.id);
    if (!data.length) {
      res.cc("未找到用户");
    }
    return res.send(data[0]);
  },
  // 修改姓名接口
  updateName: async (req, res) => {
    const { id, name } = req.body;
    if (!id || !name) {
      res.cc("参数错误");
    }
    const sql = "UPDATE users SET name = ? WHERE id = ?";
    const [data] = await db.query(sql, [name, id]);
    if (!data.affectedRows) {
      res.cc("未找到该用户");
    }
    return res.send({
      status: 1,
      message: "修改成功",
    });
  },
  // 修改性别接口
  updateSex: async (req, res) => {
    const { id, sex } = req.body;
    if (!id || !sex) {
      res.cc("参数错误");
    }
    const sql = "UPDATE users SET sex = ? WHERE id = ?";
    const [data] = await db.query(sql, [sex, id]);
    if (!data.affectedRows) {
      res.cc("未找到该用户");
    }
    return res.send({
      status: 1,
      message: "修改成功",
    });
  },
  // 修改邮箱接口
  updateEmail: async (req, res) => {
    const { id, email } = req.body;
    if (!id || !email) {
      res.cc("参数错误");
    }
    const sql = "UPDATE users SET email = ? WHERE id = ?";
    const [data] = await db.query(sql, [email, id]);
    if (!data.affectedRows) {
      res.cc("未找到该用户");
    }
    return res.send({
      status: 1,
      message: "修改成功",
    });
  },
  // 验证账号邮箱
  verifyAccountAndEmail: async (req, res) => {
    const { account, email } = req.body;
    if (!account || !email) {
      res.cc("参数错误");
    }
    const sql = "SELECT * FROM users WHERE account = ?";
    const [data] = await db.query(sql, account);
    if (!data.length) {
      return res.cc("用户名或邮箱填写错误");
    }
    if (email !== data[0].email) {
      return res.cc("验证失败");
    }
    res.send({
      status: 1,
      message: "验证成功",
      data: data[0].id,
    });
  },
  // 登录页修改密码接口
  updatePasswordLogin: async (req, res) => {
    let { newPassword, id } = req.body;
    newPassword = bcrypt.hashSync(newPassword, 10);
    const sql = "UPDATE users SET password = ? WHERE id = ?";
    const [data] = await db.query(sql, [newPassword, id]);
    if (!data.affectedRows) {
      res.cc("未找到该用户");
    }
    res.send({
      status: 1,
      message: "修改成功",
    });
  },
};

module.exports = userInfoController;
